package org.zch.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zch.dao.AdminDAO;
import org.zch.entity.po.AdminInfoPO;
import org.zch.service.AdminService;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 管理员
 * 方法实现层
 *
 * @author 赵昌虎
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDAO adminDAO;

    @Override
    public Integer addAdmin(String name, String password) {
        AdminInfoPO adminInfoPO = new AdminInfoPO();
        adminInfoPO.setName(name);
        adminInfoPO.setPassword(password);
        adminInfoPO.setType("1");
        adminInfoPO.setCreateTime(new Date());
        adminInfoPO.setLastTime(new Date());
        return adminDAO.insert(adminInfoPO);
    }


    @Override
    public Integer upAdminPassWord(String password, String name) {
        return adminDAO.upAdminPassWord(password, name);
    }

    @Override
    public Integer deleteAdmin(String name) {
        return adminDAO.deleteAdmin(name);
    }

    @Override
    public Integer upLastTime(String name) {
        return adminDAO.upLastTime(name, new Date());
    }

    @Override
    public AdminInfoPO getAdmin(String name) {
        return adminDAO.getAdmin(name);
    }

    @Override
    public String loginAdmin(String name, String password) {
        String mS = "登录成功";
        AdminInfoPO mD = getAdmin(name);
        if (mD != null) {
            if (!Objects.equals(mD.getPassword(), password)) {
                mS = "密码错误";
            } else {
                if (Objects.equals(mD.getType(), "0")) {
                    mS += "HL0";

                }
            }
        } else {
            mS = "当前管理员不存在";
        }
        return mS;
    }


    @Override
    public List<AdminInfoPO> findAll() {
        return adminDAO.findAll();
    }
}
